const { Controller } = require('egg');
class BaseController extends Controller {
    /**
     * 
     * 获取登录用户信息
     * @readonly
     * @memberof BaseController
     */
    get user() {
        return this.ctx.state.user || {
            userid: '11111111-1111-1111-1111-111111111111',
            username: '陈波',
        };
    }

    /**
     * 获取http请求参数
     * 
     * @readonly
     * @memberof BaseController
     */
    get param() {
        let param = {};//
        param = Object.assign(param, this.ctx.query, this.ctx.params, this.ctx.request.body);
        return param;
    }

    /**
     * 请求成功方法
     * 
     * @param {any} data 
     * @memberof BaseController
     */
    success(data) {
        this.ctx.body = { success: true, data };
    }

    /**
     * 请求失败方法
     * 
     * @param {any} message 
     * @memberof BaseController
     */
    fail(message) {
        this.ctx.body = { success: false, message };
    }

    /**
     * 生成分页参数
     * 
     * @param {any} param 
     * @memberof BaseController
     */
    createPageParam(param) {
        if (!param) return
        if (param.page == null) param.page = 1
        if (param.limit == null) param.limit = 20
        param.offset = (param.page - 1) * param.limit
    }
}
module.exports = BaseController;